package com.wudi.estate.service.impl.servicemode;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.servicemode.Activity;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.mapper.servicemode.ActivityMapper;
import com.wudi.estate.service.servicemode.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private CommunityMapper communityMapper;
    //查询所用活动（查询用）
    @Override
    public List<Activity> selectAll() throws Exception {
        List<Activity> activities = activityMapper.selectList(null);
       return activities;
    }
//    添加一个活动（添加用）
    @Override
    public void insert(Activity activity) throws Exception {
        activityMapper.insert(activity);
    }
    //根据id查询活动
    @Override
    public Activity selectById(long id) throws Exception {
        return  activityMapper.selectById(id);
    }
//    根据id更改活动（编辑用）
    @Override
    public void update(Activity activity) {
        Activity activity1 = activityMapper.selectById(activity.getId());
        activity1.setCid(activity.getCid());
        activity1.setTitle(activity.getTitle());
        activity1.setLocation(activity.getLocation());
        activity1.setUnit(activity.getUnit());
        activity1.setBeginTime(activity.getBeginTime());
        activity1.setEndTime(activity.getEndTime());
        activity1.setState(activity.isState());
        activityMapper.updateById(activity1) ;
    }
//  把指定的id的状态更改（删除用）
    @Override
    public void deleteById(long id) throws Exception {
        Activity activity = activityMapper.selectById(id);
        activity.setState(false);
        activityMapper.updateById(activity);
    }
//查询活动和小区列表（添加显示用）
    @Override
    public List<Map<String, Object>> selectActivityAndCommunity() throws Exception {
        List<Map<String,Object>> lists = new ArrayList<>();
        List<Activity> activities = activityMapper.selectList(null);
        for (Activity activity : activities) {
            Map<String,Object> maps = new HashMap<>();
            maps.put("activity",activity);
            Community community = communityMapper.selectById(activity.getCid());
            maps.put("community",community);
            lists.add(maps);
        }
        return lists;
    }
    //根据id查询活动和小区列表（编辑显示用）
    @Override
    public Map<String, Object> selectActivityAndCommunityById(long id) throws  Exception{
        Map<String,Object> maps = new HashMap<>();
        Activity activity = activityMapper.selectById(id);
        List<Community> communitys = communityMapper.selectList(null);
        maps.put("communitys",communitys);
        maps.put("activity",activity);
        return maps;
    }
//根据名字模糊查询
    @Override
    public List<Map<String, Object>> selectActivityAndCommunityByLike(String title, Date beginTime,Date endTime) throws Exception {
        List<Map<String,Object>> lists = new ArrayList<>();
        QueryWrapper<Activity> queryWrapper = new QueryWrapper();
        if(title!=null){
            queryWrapper.like("title",title);
        }
        if(beginTime!=null){
            queryWrapper.gt("begin_time",beginTime);
        }
        if(endTime!=null){
            queryWrapper.lt("end_time",endTime);
        }
        List<Activity> activities = activityMapper.selectList(queryWrapper);
        for (Activity activity : activities) {
            Map<String,Object> maps = new HashMap<>();
            maps.put("activity",activity);
            Community community = communityMapper.selectById(activity.getCid());
            maps.put("community",community);
            lists.add(maps);
        }
        return lists;
    }

    @Override
    public PageInfo<Map<String, Object>> selectActivityAndCommunityPage(int pageNum, int limit) {
        PageHelper.startPage(pageNum, limit);
        PageInfo<Map<String, Object>> pageInfo=null;
        try {
            List<Map<String, Object>> lists = activityMapper.selectActivityAndCommunity();
            pageInfo  = new PageInfo<>(lists);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Map<String, Object>> selectActivityAndCommunityByLikeAndPage(String title, Date beginTime, Date endTime, Integer pageNum, int limit) {
        List<Map<String,Object>> lists = new ArrayList<>();
        QueryWrapper<Activity> queryWrapper = new QueryWrapper();
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
        String begin = "";
        String end="";
        PageHelper.startPage(pageNum, limit);
        if(title!=null){
            queryWrapper.like("title",title);
        }
        if(beginTime!=null){
            queryWrapper.gt("begin_time",beginTime);
            begin  = format.format(beginTime);
        }
        if(endTime!=null){
            queryWrapper.lt("end_time",endTime);
            end= format.format(endTime);
        }
        try {
           lists=activityMapper.selectActivityAndCommunityByPageAndLike(begin,end,title);

        } catch (Exception e) {
            e.printStackTrace();
        }
            PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

//    @Override
//    public PageInfo<Map<String,Object>> selectActivityAndCommunityPage(int pageNum,int  limit) {
//        PageHelper.startPage(pageNum,limit);
//        List<Map<String,Object>> lists = new ArrayList<>();
//        List<Activity> activities = activityMapper.selectList(null);
//        for (Activity activity : activities) {
//            Map<String,Object> maps = new HashMap<>();
//            maps.put("activity",activity);
//            Community community = communityMapper.selectById(activity.getCid());
//            maps.put("community",community);
//            lists.add(maps);
//        }
//        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(lists);
//        return pageInfo;
//    }

}
